In this article you will get the knowledge about Send Email via SMTP Server in PHP using PHPMailer. Send email from PHP Script is the most popular feature for the web application. Basically mail() is used to send email in php. When you try to send email using the php mail() function, the mail is sent from your web server. Sometimes it may create issue on sending an email and fails to deliver mail to recipient email id. But with the help of SMTP you can overcome this issue, SMTP is the most recommended way to send email from the php script. When you try to send an email vis SMTP, email is send from the mail server rather than the web server.
PHPMailer library provides the easiest way to send email in php with SMTP. PHPMailer library have various configuration options that allow you to send a text email, HTML mail and attachment.
Now, you will see how you can send HTML email with SMTP using PHPMailer in PHP. You can use the following code to send SMTP mail using PHPMailer library.
index.php
Use isHTML(true) to set email format to HTML
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<?php // Include and initialize phpmailer class require 'PHPMailer/PHPMailerAutoload.php'; $mail = new PHPMailer; // SMTP configuration $mail->isSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'user@example.com'; $mail->Password = '******'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->setFrom('info@example.com', 'Codepylly'); $mail->addReplyTo('info@example.com', 'Codepully'); // Add a recipient $mail->addAddress('john@gmail.com'); // Add cc or bcc $mail->addCC('cc@example.com'); $mail->addBCC('bcc@example.com'); // Email subject $mail->Subject = 'Send Email via SMTP using PHPMailer'; // Set email format to HTML $mail->isHTML(true); // Email body content $mailContent = "<h1>Send HTML Email using SMTP in PHP</h1> <p>This is a test email has sent using SMTP mail server with PHPMailer.</p>"; $mail->Body = $mailContent; // Send email if(!$mail->send()){ echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; }else{ echo 'Message has been sent'; } ?> |
Send Email with Attachments:
Use addAttachment() method of PHPMailer class to add an attachment or multiple attachments to the email.
2 3 4 5 6 7 |
// Add attachments $mail->addAttachment('docs/attachment_1.pdf'); $mail->addAttachment('docs/attachment_2.docs'); $mail->addAttachment('images/attachment_3.png', 'attachment4.png'); //set new name |
Send HTML Email to Multiple Recipients:
Add addAddress() method multiple times for sending same email to the multiple recipients.
2 3 4 5 6 |
// Add multiple recipients $mail->addAddress('abc@gmail.com'); $mail->addAddress('xyz@gmail.com'); |
NOTE: Are you want to get implementation help, or modify or extend the functionality of this script? please leave your comment in below comment section.
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co